from nodetypes.core.ft.Account import Manager
from myelin.server.servers.ft.http.Module import Module

from Ft.Server.Server import FTSERVER_SERVER_NS
from Ft.Server.Common.Util import RepoPathToUri

class Module(Module):
    
    commands = {
        (FTSERVER_SERVER_NS, 'AllowDomain'): 'allowDomain',
    }
    
    newManager = Manager
    
    def startConfig(self, parser, config, name, attrs):
        try:
            self.rootUri = str(attrs[(None,'root-uri')])
        except KeyError:
            raise ValueError("%s requires a non-empty accounts root-uri attribute: %s" % (name, attrs))
        
        #try:
        #    self.acctModelUri  = RepoPathToUri(attrs[(None,'accounts-model')].encode('ascii'))
        #except KeyError:
        #    raise ValueError("%s requires a non-empty accounts model name attribute: %s" % (name, attrs))
        #
        #try:
        #    self.aclsModelUri  = RepoPathToUri(attrs[(None,'acls-model')].encode('ascii'))
        #except KeyError:
        #    raise ValueError("%s requires a non-empty acls model name attribute: %s" % (name, attrs))
                        
        self.domains = {}
        # add allowed domains
        # for domain, admins and users group defined
     
    def endConfig(self, parser, config, name, attrs):
        config.accountsMgr = self.newManager(
            self.rootUri,
            self.domains
            #,
            #self.acctModelUri,
            #self.aclsModelUri
        )
    
    
    def allowDomain(self, parser, config, name, data, attrs):
        try:
            domainNick = attrs[(None,'nick')]
            domainName = attrs[(None,'name')]
            adminsName = attrs[(None,'admins-group-name')]
            #usersName = attrs[(None,'users-group-name')]
        except:
            raise ValueError("%s requires a non-empty domain, admins, and users name attributes: %s" % (name, attrs))
        
        #if not len(data):
        #    raise ValueError("%s requires a non-empty data value as its domain id" % name)
        
        self.domains[domainNick] = (domainName, adminsName) # usersName,)
        